package com.app.controller.supermarket;


import com.app.dto.supermarket.MyCollectDto;
import com.app.mapper.supermarket.MyCollectMapper;
import com.app.model.supermarket.MktPersonModel;
import com.app.model.supermarket.MyCollectModel;
import com.app.model.supermarket.OddJobReleaseModel;
import com.app.service.supermarket.MktPersonService;
import com.app.service.supermarket.MyCollectService;
import com.app.util.*;
import com.app.vo.supermarket.MyCollectVo;
import com.app.vo.supermarket.OddJobReleaseReturnVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;


@Controller
@RestController
public class MyCollectController {


    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    private MyCollectService myCollectService;
    @Autowired
    private MyCollectMapper myCollectMapper;
    @Autowired
    MktPersonService mktPersonService;

    //增
    @GetMapping ("/insertMyCollect")
    @ResponseBody
    public Result<Object> insertMyCollect(@RequestParam("keyId") int keyId,
                                  @RequestParam("type") int type,
                                  HttpServletRequest request
    ) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        MyCollectModel model = new MyCollectModel();
        model.setKeyId(keyId);
        model.setMid(mktPerson.getId());
        model.setType(type);

        QueryWrapper<MyCollectDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("key_id", keyId).eq("type", type).eq("mid", model.mid);
        List<MyCollectDto> myCollects = myCollectMapper.selectList(queryWrapper);
        if (myCollects.size() > 0) {
           return Result.success("已收藏请勿重复收藏");
        }else{
            model.setCrtTime(CommonHelp.getNowTimestamp());
            myCollectMapper.insert(model);

        }
       return Result.success("操作成功");

    }

    //取消收藏
    @GetMapping("/delMyCollect")
    @ResponseBody
    public Result<Object> delMyCollect(HttpServletRequest request,
                               @RequestParam("oid") int oid) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }

        MyCollectModel myCollect = myCollectMapper.selectByOid(oid,mktPerson.getId());
        if(myCollect == null){
            return Result.error(500,"不存在");
        }
         myCollectMapper.deleteByOid(oid,mktPerson.getId());
      return Result.success("操作成功");
    }

    /**
     * 根据type查询我的收藏
     *
     * @param request
     * @return
     * @throws ParseException
     */

    @GetMapping("/getMyCollectList")
    public Result<Object> getMyCollectList(HttpServletRequest request) throws ParseException {
        String type = request.getParameter("type");
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel customer = mktPersonService.getModelByMobile(mobile);
        if(customer==null)
        {
            return Result.error(405,"用户不存在");
        }

        MyCollectModel model = new MyCollectModel();
        model.setType(Integer.valueOf(type));
        model.setMid(customer.getId());
        List<MyCollectModel> modelList;
        if (model.getType() == 1) {
            modelList = myCollectMapper.getCollectByOid(model);
        } else {
            modelList = myCollectMapper.getCollectByMid(model);
        }

        if (modelList != null && modelList.size() > 0) {
            for (int i = 0; i < modelList.size(); i++) {
                if ("2".equals(type)) {
                    Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(modelList.get(i).getMkt().getBirthday() + "-01").getTime());
                    String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
                    modelList.get(i).getMkt().setAge(date);
                    if (modelList.get(i).getMkt().getSex() == 1) {
                        modelList.get(i).getMkt().setSexText("男");
                    } else {
                        modelList.get(i).getMkt().setSexText("女");
                    }
                } else if ("1".equals(type)) {
                    if (modelList.get(i).getOdd().getState() == 1) {
                        modelList.get(i).getOdd().setStateName("招工中");
                    }
                    if (modelList.get(i).getOdd().getState() == 2) {
                        modelList.get(i).getOdd().setStateName("待结算");
                    }
                    if (modelList.get(i).getOdd().getState() == 3) {
                        modelList.get(i).getOdd().setStateName("已完工");
                    }
                    if (modelList.get(i).getOdd().getState() == 4) {
                        modelList.get(i).getOdd().setStateName("取消");
                    }

                }
                OddJobReleaseModel odd = modelList.get(i).odd;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Timestamp data1 = Timestamp.valueOf(odd.getStartTime());//开始时间
                Timestamp data2 = Timestamp.valueOf(odd.getEndTime());//开始时间
                String startTime = sdf.format(data1);
                String endTime = sdf.format(data2);
                odd.setStartTime(startTime);
                odd.setEndTime(endTime);
            }

        }
        int  countMId = myCollectMapper.countMid(customer.getId());
        MyCollectVo vo = new MyCollectVo();
        vo.setCount(countMId);
        vo.setList(modelList);
        return Result.success(vo);
    }
    @ApiOperation(value = "收藏状态")
    @RequestMapping(value = "/getCollectStatus", method = RequestMethod.GET)
    public Result<Object> getStatus(Integer oid,Integer status,HttpServletRequest request){
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        QueryWrapper<MyCollectDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("keyId", oid).eq("type", 1).eq("mid", mktPerson.getId());
        List<MyCollectDto> myCollects = myCollectMapper.selectList(queryWrapper);
        return Result.success(myCollects);
    }
}
